home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / net.s5 / tcpcli.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  97 lines

  1. /*
  2.  * Example of client using TCP protocol.
  3.  */
  4.  
  5. #include    "inet.h"
  6.  
  7. main(argc, argv)
  8. int    argc;
  9. char    *argv[];
  10. {
  11.     int            tfd;
  12.     char            *t_alloc();    /* TLI function */
  13.     struct t_call        *callptr;
  14.     struct sockaddr_in    serv_addr;
  15.  
  16.     pname = argv[0];
  17.  
  18.     /*
  19.      * Create a TCP transport endpoint and bind it.
  20.      */
  21.  
  22.     if ( (tfd = t_open(DEV_TCP, O_RDWR, 0)) < 0)
  23.         err_sys("client: can't t_open %s", DEV_TCP);
  24.  
  25.     if (t_bind(tfd, (struct t_bind *) 0, (struct t_bind *) 0) < 0)
  26.         err_sys("client: t_bind error");
  27.  
  28.     /*
  29.      * Fill in the structure "serv_addr" with the address of the
  30.      * server that we want to connect with.
  31.      */
  32.  
  33.     bzero((char *) &serv_addr, sizeof(serv_addr));
  34.     serv_addr.sin_family      = AF_INET;
  35.     serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR);
  36.     serv_addr.sin_port        = htons(SERV_TCP_PORT);
  37.  
  38.     /*
  39.      * Allocate a t_call structure, and initialize it.
  40.      * Let t_alloc() initialize the addr structure of the t_call structure.
  41.      */
  42.  
  43.     if ( (callptr = (struct t_call *) t_alloc(tfd, T_CALL, T_ADDR)) == NULL)
  44.         err_sys("client: t_alloc error");
  45.     callptr->addr.maxlen = sizeof(serv_addr);
  46.     callptr->addr.len    = sizeof(serv_addr);
  47.     callptr->addr.buf    = (char *) &serv_addr;
  48.     callptr->opt.len     = 0;        /* no options */
  49.     callptr->udata.len   = 0;        /* no user data with connect */
  50.  
  51.     /*
  52.      * Connect to the server.
  53.      */
  54.  
  55.     if (t_connect(tfd, callptr, (struct t_call *) 0) < 0)
  56.         err_sys("client: can't t_connect to server");
  57.  
  58.     doit(stdin, tfd);    /* do it all */
  59.  
  60.     close(tfd);
  61.     exit(0);
  62. }
  63.  
  64. /*
  65.  * Read the contents of the FILE *fp, write each line to the
  66.  * transport endpoint (to the server process), then read a line back from
  67.  * the transport endpoint and print it on the standard output.
  68.  */
  69.  
  70. doit(fp, tfd)
  71. register FILE    *fp;
  72. register int    tfd;
  73. {
  74.     int    n, flags;
  75.     char    sendline[MAXLINE], recvline[MAXLINE + 1];
  76.  
  77.     while (fgets(sendline, MAXLINE, fp) != NULL) {
  78.         n = strlen(sendline);
  79.         if (t_snd(tfd, sendline, n, 0) != n)
  80.             err_sys("client: t_snd error");
  81.  
  82.         /*
  83.          * Now read a line from the transport endpoint and write it to
  84.          * our standard output.
  85.          */
  86.  
  87.         n = t_rcv(tfd, recvline, MAXLINE, &flags);
  88.         if (n < 0)
  89.             err_dump("client: t_rcv error");
  90.         recvline[n] = 0;    /* null terminate */
  91.         fputs(recvline, stdout);
  92.     }
  93.  
  94.     if (ferror(fp))
  95.         err_sys("client: error reading file");
  96. }
  97.